package com.yida.gwj.expend.outside.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yida.common.core.exception.ServiceException;
import com.yida.gwj.common.domain.mq.MqSendResult;
import com.yida.gwj.common.enums.GwjBusinessFlagType;
import com.yida.gwj.common.handler.MqSendMessageHandler;
import com.yida.gwj.common.utils.GwjEnumUtils;
import com.yida.gwj.expend.outside.config.MaintenanceFundsBusinessConfiguration;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessBuild;
import com.yida.gwj.expend.outside.domain.GwjMaintenanceFundsBusinessHouse;
import com.yida.gwj.expend.outside.domain.dto.AcceptanceReportDTO;
import com.yida.gwj.expend.outside.domain.dto.AllocatedAmountListDTO;
import com.yida.gwj.expend.outside.domain.dto.AllocatedAmountSelectDTO;
import com.yida.gwj.expend.outside.domain.dto.ApplyAppropriateReportDTO;
import com.yida.gwj.expend.outside.domain.dto.GetBusinessDetailDTO;
import com.yida.gwj.expend.outside.domain.dto.GwjMaintenanceFundsBusinessListDTO;
import com.yida.gwj.expend.outside.domain.dto.StepConfigInfoDTO;
import com.yida.gwj.expend.outside.domain.dto.TotalCostShareListDTO;
import com.yida.gwj.expend.outside.domain.message.MaintenanceFundsMqMessageBody;
import com.yida.gwj.expend.outside.domain.message.PreAcceptSubmitMessage;
import com.yida.gwj.expend.outside.domain.request.AllocatedAmountRequest;
import com.yida.gwj.expend.outside.domain.request.GwjMaintenanceFundsBusinessListRequest;
import com.yida.gwj.expend.outside.domain.request.HousesUnderBuildRequest;
import com.yida.gwj.expend.outside.domain.request.SaveAppropriateApplyRequest;
import com.yida.gwj.expend.outside.domain.request.ShareEquallyMoneyRequest;
import com.yida.gwj.expend.outside.enums.BusinessAttachmentServerTypeEnum;
import com.yida.gwj.expend.outside.enums.FlagStatusEnum;
import com.yida.gwj.expend.outside.enums.MaintenanceFundsErrorEnum;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessAppropriateService;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessService;
import com.yida.gwj.expend.outside.service.IGwjMaintenanceFundsBusinessUrgentAppropriateService;

import cn.hutool.json.JSONUtil;

/**
 * gwj应急拨付业务
 *
 * @author wkz
 * @date 2023/04/10
 */
@Service

public class GwjMaintenanceFundsBusinessUrgentAppropriateServiceImpl implements IGwjMaintenanceFundsBusinessUrgentAppropriateService {

    private final GwjBusinessFlagType businessFlagType = GwjBusinessFlagType.EXPEND_OUTSIDE_URGENT_APPROPRIATE;
    @Autowired
    private IGwjMaintenanceFundsBusinessAppropriateService gwjMaintenanceFundsBusinessAppropriateService;
    @Autowired
    private IGwjMaintenanceFundsBusinessService maintenanceFundsBusinessService;
    @Autowired
    private MaintenanceFundsBusinessConfiguration maintenanceFundsBusinessConfiguration;
    @Autowired
    private MqSendMessageHandler mqSendMessageHandler;

    /**
     * 查询公维金拨付申请业务列表
     *
     * @param maintenanceFundsBusinessListRequest 公维金维修资金业务
     * @return 公维金维修资金业务
     */
    @Override
    public List<GwjMaintenanceFundsBusinessListDTO> getGwjMaintenanceFundsBusinessList(GwjMaintenanceFundsBusinessListRequest maintenanceFundsBusinessListRequest) {
        return maintenanceFundsBusinessService.getGwjMaintenanceFundsBusinessList(maintenanceFundsBusinessListRequest);
    }

    /**
     * 获取gwj维修基金业务详细信息
     *
     * @param bizId 业务标识
     * @return {@link GetBusinessDetailDTO}
     */
    @Override
    public GetBusinessDetailDTO getDetailMessage(Long bizId) {
        return maintenanceFundsBusinessService.getDetailMessage(bizId);
    }

    /**
     * 分摊金额-已选择
     *
     * @param allocatedAmountRequest
     * @return AllocatedAmountSelectDTO
     */
    @Override
    public AllocatedAmountSelectDTO allocatedAmountList(AllocatedAmountRequest allocatedAmountRequest) {
        return maintenanceFundsBusinessService.allocatedAmountList(allocatedAmountRequest, GwjBusinessFlagType.EXPEND_OUTSIDE_URGENT_APPROPRIATE);
    }

    /**
     * 资金分摊-未选中列表
     *
     * @param allocatedAmountRequest 未选中列表
     * @return 公维金维修资金业务
     */
    @Override
    public List<GwjMaintenanceFundsBusinessHouse> allocatedAmountCancelList(AllocatedAmountRequest allocatedAmountRequest) {
        return maintenanceFundsBusinessService.allocatedAmountCancelList(allocatedAmountRequest);
    }

    /**
     * 按类型平摊金额 -计算标准
     *
     * @param shareEquallyMoneyRequest
     * @return AllocatedAmountSelectDTO
     */
    @Override
    public int shareEquallyMoney(ShareEquallyMoneyRequest shareEquallyMoneyRequest) {
        return gwjMaintenanceFundsBusinessAppropriateService.shareEquallyMoney(shareEquallyMoneyRequest);
    }

    /**
     * 修改平摊金额
     *
     * @param shareEquallyMoneyRequest
     * @return AllocatedAmountSelectDTO
     */
    @Override
    public int shareEquallyMoneyUpdate(ShareEquallyMoneyRequest shareEquallyMoneyRequest) {
        return maintenanceFundsBusinessService.shareEquallyMoney(shareEquallyMoneyRequest);
    }

    /**
     * 获取验收报告
     *
     * @param bizId 业务标识
     * @return {@link AcceptanceReportDTO}
     */
    @Override
    public AcceptanceReportDTO getAcceptanceReport(Long bizId) {
        return gwjMaintenanceFundsBusinessAppropriateService.getAcceptanceReport(bizId);
    }

    /**
     * 保存应急拨付申请业务
     *
     * @param saveAppropriateApplyRequest 保存拨付申请业务请求
     * @return int
     */
    @Override
    public int saveAppropriateApply(SaveAppropriateApplyRequest saveAppropriateApplyRequest) {
        return gwjMaintenanceFundsBusinessAppropriateService.saveAppropriateApply(saveAppropriateApplyRequest);
    }

    /**
     * 分摊金额-选中房屋
     *
     * @param allocatedAmountRequest
     * @return AllocatedAmountSelectDTO
     */
    @Override
    public int allocatedAmountArrange(AllocatedAmountRequest allocatedAmountRequest) {
        return maintenanceFundsBusinessService.allocatedAmountArrange(allocatedAmountRequest);
    }

    /**
     * 分摊金额-取消选中房屋
     *
     * @param allocatedAmountRequest
     * @return AllocatedAmountSelectDTO
     */
    @Override
    public int allocatedAmountCancel(AllocatedAmountRequest allocatedAmountRequest) {
        return gwjMaintenanceFundsBusinessAppropriateService.allocatedAmountCancel(allocatedAmountRequest);
    }

    /**
     * 获取应急拨付报告
     *
     * @param bizId 业务标识
     * @return {@link ApplyAppropriateReportDTO}
     */
    @Override
    public ApplyAppropriateReportDTO getApplyAppropriateReport(Long bizId) {
        return gwjMaintenanceFundsBusinessAppropriateService.getApplyAppropriateReport(bizId);
    }

    /**
     * 预受理提交
     *
     * @param bizId 业务标识
     * @return int
     */
    @Override
    public int preSubmit(Long bizId) {
        PreAcceptSubmitMessage preAcceptSubmitMessage = maintenanceFundsBusinessService.preAcceptSubmit(bizId, BusinessAttachmentServerTypeEnum.getAppropriateServerTypeList());
        String tag = maintenanceFundsBusinessConfiguration.urgentAppropriateTagProperties().getPreAcceptSubmit();
        MqSendResult mqSendResult = mqSendMessageHandler.syncSend(new MaintenanceFundsMqMessageBody() {
            {
                setJsonMsg(JSONUtil.toJsonStr(preAcceptSubmitMessage));
                setServerIdentification(preAcceptSubmitMessage.getMaintenanceFundsBusiness().getServerIdentification());
            }
        }, new MaintenanceFundsBusinessConfiguration.MaintenanceFundsBusinessUrgentAppropriateSendRequest(tag, null));
        if (mqSendResult.isSuccess()) {
            return FlagStatusEnum.SUC.getCode();
        } else {
            throw new ServiceException(GwjEnumUtils.getEnumConstantMsg(MaintenanceFundsErrorEnum.URGENT_APPROPRIATE_PRE_ACCEPT_SUBMIT) + "公维金资金申请提交数据到内网MQ异常");
        }
    }

    /**
     * 查询决算费用分摊清册
     *
     * @param bizId 业务标识
     * @return int
     */
    @Override
    public TotalCostShareListDTO costList(Long bizId) {
        return gwjMaintenanceFundsBusinessAppropriateService.costList(bizId, BusinessAttachmentServerTypeEnum.FINAL_COST_ALLOCATION_LIST);
    }

    /**
     * 获取已选中楼栋信息
     *
     * @param bizId
     * @return 获取已选中楼栋信息
     */
    @Override
    public List<GwjMaintenanceFundsBusinessBuild> getSelectBuilds(Long bizId) {
        return maintenanceFundsBusinessService.getSelectBuilds(bizId);
    }

    /**
     * 获取楼栋下房屋信息
     *
     * @param housesUnderBuildRequest
     * @return 楼栋下房屋信息
     */
    @Override
    public List<GwjMaintenanceFundsBusinessHouse> getHouseList(HousesUnderBuildRequest housesUnderBuildRequest) {
        return maintenanceFundsBusinessService.getHouseList(housesUnderBuildRequest);
    }

    /**
     * 导出房屋数据
     *
     * @param allocatedAmountRequest
     * @return 楼栋下房屋信息
     */
    @Override
    public List<AllocatedAmountListDTO> exportHouseList(AllocatedAmountRequest allocatedAmountRequest) {
        return maintenanceFundsBusinessService.exportHouseList(allocatedAmountRequest);
    }

    @Override
    public StepConfigInfoDTO getStepConfigInfo() {
        return maintenanceFundsBusinessService.getStepConfigInfo(businessFlagType);
    }
}
